package top.tagao.m03_beHavioralPatterns.b_05_mediator.mediator;

import top.tagao.m03_beHavioralPatterns.b_05_mediator.colleague.*;

import java.util.HashMap;

/**
 * 具体中介者
 * @author Viktor_Liu
 * @Date 2022/8/13 23:14
 */
public class ConcreteMediator extends Mediator{
    //集合，放入所有的同事对象
    private HashMap<String, Colleague> colleagueMap;
    private HashMap<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<String, Colleague>();
        interMap = new HashMap<String, String>();
    }

    @Override
    public void Register(String colleagueName, Colleague colleague) {

        colleagueMap.put(colleagueName, colleague);



        if (colleague instanceof AlarmColleague) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof CoffeeColleague) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof TVColleague) {
            interMap.put("TV", colleagueName);
        } else if (colleague instanceof CurtainsColleague) {
            interMap.put("Curtains", colleagueName);
        }

    }

    //具体中介者的核心方法
    //1. 根据得到消息，完成对应任务
    //2. 中介者在这个方法，协调各个具体的同事对象，完成任务
    @Override
    public void GetMessage(int stateChange, String colleagueName) {
        // TODO Auto-generated method stub

        //处理闹钟发出的消息
        if (colleagueMap.get(colleagueName) instanceof AlarmColleague) {
            if (stateChange == 0) {
                ((CoffeeColleague) (colleagueMap.get(interMap
                        .get("CoffeeMachine")))).StartCoffee();
                ((TVColleague) (colleagueMap.get(interMap.get("TV")))).StartTv();
            } else if (stateChange == 1) {
                ((TVColleague) (colleagueMap.get(interMap.get("TV")))).StopTv();
            }

        } else if (colleagueMap.get(colleagueName) instanceof CoffeeColleague) {
            ((CurtainsColleague) (colleagueMap.get(interMap.get("Curtains"))))
                    .UpCurtains();

        } else if (colleagueMap.get(colleagueName) instanceof TVColleague) {//如果TV发现消息

        } else if (colleagueMap.get(colleagueName) instanceof CurtainsColleague) {
            //如果是以窗帘发出的消息，这里处理...
        }

    }

    @Override
    public void SendMessage() {
        // TODO Auto-generated method stub

    }
}
